package com.maixy.studyonlineapi.controller;

import com.maixy.studyonlineapi.apis.ApiController;
import com.maixy.studyonlineapi.apis.ApiRequest;
import com.maixy.studyonlineapi.apis.ApiResponse;
import com.maixy.studyonlineapi.apis.Privilege;
import com.maixy.studyonlineapi.bean.Page;
import com.maixy.studyonlineapi.exceptions.MissParamException;
import com.maixy.studyonlineapi.result.PageData;
import com.maixy.studyonlineapi.service.MomentService;
import com.maixy.studyonlineapi.utils.DataTypeUtil;
import com.maixy.studyonlineapi.view.MomentView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @ClassName MomentController
 * @Description TODO
 * @Date 2021/2/7 23:23
 * @Author maixy
 */
@RestController
@RequestMapping("/v1/moment")
public class MomentController extends ApiController {

    @Resource
    private MomentService momentService;

    /**
     * 添加打卡
     *
     * @param request -- activityId 活动id
     *                -- content 内容id
     * @return com.maixy.studyonlineapi.apis.ApiResponse
     */
    @Privilege
    public ApiResponse addMoment(ApiRequest request) {
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        Long activityId = request.getDataParamAsLong("activityId");
        String content = request.getDataParamAsString("content");

        if (StringUtils.isBlank(content) || null == activityId) {
            throw new MissParamException();
        }
        momentService.addMoment(userId, activityId, content);
        return ApiResponse.ok();
    }

    /**
     * 删除打卡
     *
     * @param request -- momentId 打卡id
     * @return com.maixy.studyonlineapi.apis.ApiResponse
     */
    @Privilege
    public ApiResponse delMoment(ApiRequest request) {
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        Long momentId = request.getDataParamAsLong("momentId");

        if (null == momentId) {
            throw new MissParamException();
        }
        momentService.delMoment(userId, momentId);
        return ApiResponse.ok();
    }

    /**
     * 获得打卡列表
     *
     * @param request -- activityId 活动id
     * @return com.maixy.studyonlineapi.apis.ApiResponse
     */
    @Privilege
    public ApiResponse getMomentList(ApiRequest request) {
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        Long activityId = request.getDataParamAsLong("activityId");
        Boolean hasOnlyMyMoment = request.getDataParamAsBool("hasOnlyMyMoment", false);
        Page page = Page.validation(request.getPage());

        if (null == activityId) {
            throw new MissParamException();
        }
        PageData<MomentView> pageData = momentService.getMomentList(userId, activityId, hasOnlyMyMoment, page);
        return ApiResponse.ok().addValueToData("pageData", pageData);
    }
}
